package com.android.project.framework.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;


import com.android.project.framework.impl.GLGame;
import com.android.project.games.Settings;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;

public class FacebookActivity {
	GLGame game;
	Facebook facebook = new Facebook("317069508382250");
	

	SharedPreferences settings;
	
	public FacebookActivity(GLGame game){
		this.game = game;
		 
		
	}
	
	public Facebook getFacebook() {
		return facebook;
	}
	public boolean login(){
		settings = game.getPreferences(0);
		String access_token = settings.getString("access_token", null);
		long expires = settings.getLong("access_expires", 0);
		 if(access_token != null) {
	            facebook.setAccessToken(access_token);
        }
        if(expires != 0) {
            facebook.setAccessExpires(expires);
        }

       
        /*
         * Only call authorize if the access_token has expired.
         */
        if(!facebook.isSessionValid()) {
        	game.runOnUiThread(new Runnable() {
        		
        		//@Override
        		public void run() {
        			// TODO Auto-generated method stub
        			facebook.authorize(game,  new String[] { "email", "publish_checkins", "publish_actions", "read_stream" }, new DialogListener(){
        				@Override
        	            public void onComplete(Bundle values) {
        					 SharedPreferences.Editor editor = settings.edit();
        	                    editor.putString("access_token", facebook.getAccessToken());
        	                    editor.putLong("access_expires", facebook.getAccessExpires());
        	                    editor.commit();
        	                    Settings.facebook = true;
        	                    Settings.save(game.getFileIO());
        	                    
        					/*Bundle params = new Bundle();
        					params.putString("message", "whatever");
        					facebook.dialog(glGame.getApplicationContext(), "feed", params, new PostDialogListener());*/
        				}

        	            @Override
        	            public void onFacebookError(FacebookError error) {}

        	            @Override
        	            public void onError(DialogError e) {}

        	            @Override
        	            public void onCancel() {}
        			});
        		}
        		});
        	return false;
        	
        }
		return true;
		
	}
	
	public void postWall( final String score){
		 game.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					 String response = null;
					// TODO Auto-generated method stub
					 //facebook.dialog(game, "feed", new PostDialogListener());
					 Bundle params = new Bundle();
 					params.putString("message", "Ho appena giocato a Words Chain! Ho totalizzato il punteggio di " + score);
 					//facebook.dialog(game.getApplicationContext(), "feed", params, new PostDialogListener());
 				try {
 					  response =	facebook.request("me/feed", params, "POST");
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
 				if(response != "false" || response != null){
	                
	                 Log.d("facebook response", "Status Updated Successfully!");
				 }
					
				}
				 
			});
	}

}
